import axios from 'axios'
import { Message, MessageBox } from 'element-ui'
import Cookies from 'js-cookie'

const serivce = axios.create({
    timeout: 1000,
    // baseUrl:''
})

serivce.interceptors.request.use(config => {
    if (Cookies.get('x-token')) {
        config.headers['x-token'] = Cookies.get('x-token')
        return config
    }
    return config
}, error => {
    Message({
        message: '请求超时1000ms',
        type: 'error'
    })
})
 // 30000 请求成功，50008:非法的token; 50012:其他客户端登录了;  50014:Token 过期了;{code:50008,message:'非法TOKEN','50010':'账号密码错误'}
 serivce.interceptors.response.use(response=>{
    if(response.status == 200){
        const res = response.data
        if(res.code != 30000){
            //用户退出
            if(res.code === 40000){
                return response
            }else{
                Message({
                    message: res.msg,
                    type: 'error',
                    duration: 5 * 1000
                })
                window.location.href = '/#/auth/login'
            }
        }else{
            return response
        }
    }else{
        Message({
            message:'服务器异常，请稍后再试',
			type:'error',
			duration: 5 * 1000
        })
       
    }
},error=>{
    Message({
        message:'服务器异常，请稍后再试' + error,
        type:'error',
        duration: 5 * 1000
    })
})

export default serivce